package com.example.administrator.babymusic.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.transition.Transition;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.babymusic.R;

/**
 * Created by Administrator on 2016/11/1 0001.
 */

public class MineFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private RadioButton rb1, rb2;
    private View tabRight, tabLeft;

    private FragmentManager fManager;
    private BaseFragment fgLoaded, fgLoading;

    @Override
    public int getLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view) {
        radioGroup = (RadioGroup) view.findViewById(R.id.rg_load_bar);
        rb1 = (RadioButton) view.findViewById(R.id.rb_loaded);
        rb2 = (RadioButton) view.findViewById(R.id.rb_loading);
        rb1.setChecked(true);
        radioGroup.setOnCheckedChangeListener(this);
        tabLeft = (View) view.findViewById(R.id.tab_left_pink);
        tabRight = (View) view.findViewById(R.id.tab_right_pink);
        tabRight.setVisibility(View.GONE);


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.rb_loaded:
                tabLeft.setVisibility(View.VISIBLE);
                tabRight.setVisibility(View.GONE);
                if(fgLoaded == null) {
                    fgLoaded = new LoadedFragment();
                    fTransaction.add(R.id.music_ly_content, fgLoaded, "LoadedFragment");

                }
                else {
                    fTransaction.show(fgLoaded);
                }
                break;
            case R.id.rb_loading:
                tabLeft.setVisibility(View.GONE);
                tabRight.setVisibility(View.VISIBLE);
                if(fgLoading == null) {
                    fgLoading = new LoadingFragment();
                    fTransaction.add(R.id.music_ly_content, fgLoading, "LoadingFragment");

                }
                else {
                    fTransaction.show(fgLoading);
                }
                break;
            default:
                break;
        }
        fTransaction.commit();
    }


    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fgLoaded != null)fragmentTransaction.hide(fgLoaded);
        if(fgLoading != null)fragmentTransaction.hide(fgLoading);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fTransaction = fManager.beginTransaction();

        if(fgLoaded == null) {
            fgLoaded = new LoadedFragment();
            fTransaction.add(R.id.music_ly_content, fgLoaded, "LoadedFragment");

        }
        fTransaction.show(fgLoaded);
        fTransaction.commit();


    }
}
